package com.chenxiao.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chenxiao.aspectlogspringbootstarter.autoconfiguration.aspectlog.AspectLog;
import com.chenxiao.mybatisplus.aop.annotation.Prevent;
import com.chenxiao.mybatisplus.aop.annotation.WebLog;
import com.chenxiao.mybatisplus.entity.AjaxResult;
import com.chenxiao.mybatisplus.entity.Order;
import com.chenxiao.mybatisplus.entity.OrderDto;
import com.chenxiao.mybatisplus.entity.Record;
import com.chenxiao.mybatisplus.mapper.RecordMapper;
import com.chenxiao.mybatisplus.mq.RabbitmqProvider;
import com.chenxiao.mybatisplus.service.OrderService;
import com.chenxiao.mybatisplus.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "mybatis-join初体验")
public class AppController extends BaseController{
    @Autowired
    private OrderService orderService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private RabbitmqProvider rabbitmqProvider;
    /**
     * 分页测试
     * @param page
     * @return
     */
    @WebLog
    @Prevent(value = "40",message = "哥们，别刷了，撑不住拉")
    @AspectLog
    @ApiOperation("分页功能测试")
    @GetMapping("/queryOrderPage/{page}")
    public AjaxResult queryOrderPage(@PathVariable Long page){

        IPage<OrderDto> orderDto = orderService.queryOrderPage((IPage<OrderDto>) doPage(page));
        return new AjaxResult("200",orderDto);
    }
    @ApiOperation("分页功能测试2")
    @GetMapping("/queryOrderPages/{page}")
    public AjaxResult queryOrderPages(@PathVariable Long page){

        IPage<OrderDto> orderDto = orderService.queryOrderPage((IPage<OrderDto>) doPage(PageSize, CurrentPage));
        return new AjaxResult("200",orderDto);
    }
    @ApiOperation("订单列表")
    @GetMapping("/queryOrderList")
    public AjaxResult queryOrderList(){
        List<OrderDto> orderDto = orderService.queryOrderList();
        return new AjaxResult("200",orderDto);
    }
   @ApiOperation("记录表查询")
    @GetMapping("/queryRecordList")
   public AjaxResult queryRecordList(){
        List<Record> records = recordService.queryRecordList();
        return new AjaxResult("200",records);
    }
    @ApiOperation("记录表详情页面")
    @GetMapping("/queryRecordById/{id}")
    public AjaxResult queryRecordById(@PathVariable Long id){
        Record records = recordService.getRecordById(id);
        return new AjaxResult("200",records);
    }
    @WebLog
    @ApiOperation("记录实体查询数据")
    @PostMapping("/queryRecord")
    public AjaxResult queryRecord(@RequestBody Record record){
        QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>(record);
        List<Record> records = recordMapper.selectList(recordQueryWrapper);
        return new AjaxResult("200", records);
    }

    @ApiOperation("测试mq削峰")
    @PostMapping("/insertOrder")
    public AjaxResult insertOrder(@RequestBody Order order){
        // orderService.insertOrder(order);
        rabbitmqProvider.doPostMqMethod(order);
        return new AjaxResult("200", "新增成功");
    }
}
